package me.wpf.utils;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Description: 时间处理类
 *
 * @author 王朋飞
 * @create 2019/10/22
 */
public class DateUtils {

    public final static String DATE_FORMAT = "yyyy-MM-dd";
    public final static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_FORMAT_CHINESE = "HH:mm:ss";

    public static void main(String[] args) {
        System.out.println(dateConvertion("201910"));
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getCurDate(String DATE_FORMAT) {
        SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
        return df.format(new Date());
    }

    public static Timestamp getCurDate() {
        return new Timestamp(new Date().getTime());
    }

    public static String dateConvertion(String str) {
        String dateString = "";
        Date parse = null;
        try {
            int length = str.length();
            if (length == 4) {
                str += "0101";
            } else if (length == 6) {
                str += "01";
            }
            parse = new SimpleDateFormat("yyyyMMdd").parse(str);
            dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(parse);
        } catch (ParseException e) {
            dateString = null;
        }
        return dateString;
    }


    /**
     * 获取昨天日期 yyyyMMdd
     * @return
     */
    public static String getYesterdayByCalendar() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        return new SimpleDateFormat("yyyyMMdd").format(time);
    }
}
